package com.lagou.concurrent.demo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> future = new CompletableFuture();

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 另一个线程执行任务，将结果赋值给future
                future.complete("hello lagou");
            }
        }.start();

        System.out.println("任务已经提交");

        // 阻塞的方法
        String result = future.get();
        System.out.println(result);

    }
}
